import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class Application extends JFrame implements ActionListener {
    JTextField textField = null;

    JButton btn = null;

    public static void main(String[] args) {
        // 创建实例
        Application application = new Application();
    }

    // 构造函数
    public Application() {

        this.setTitle("选择文件窗口");
        FlowLayout layout = new FlowLayout();// 布局
        JLabel label = new JLabel("请选择文件：");// 标签
        textField = new JTextField(30);// 文本域
        btn = new JButton("浏览");// 钮1

        // 设置布局
        layout.setAlignment(FlowLayout.LEFT);// 左对齐
        this.setLayout(layout);
        this.setBounds(400, 200, 600, 70);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        btn.addActionListener(this);
        this.add(label);
        this.add(textField);
        this.add(btn);

        // 显示
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.showDialog(new JLabel(), "选择");
        File file = chooser.getSelectedFile();
        if (file != null) {
            textField.setText(file.getAbsoluteFile().toString());
            ResultModel rm = PoiUtil.calculate(file);
            String f = "单价是 %.6f\n币总数 %.6f\n 总持仓 %.6f\n";
            JOptionPane.showMessageDialog(null, String.format(f, rm.price, rm.num, rm.sum),"计算结果", JOptionPane.PLAIN_MESSAGE);
        }
    }
}
